// Created by plusminus on 9:29:06 PM - Feb 26, 2009
package org.anddev.arcc.checkers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Nicolas Gramlich
 */
public class StringValuesChecker extends AbstractBaseChecker {
	// ===========================================================
	// Constants
	// ===========================================================
	
	public static final String BASEDIRECTORY = "values";

	private static final String TAG_STRING_START = "<string name=\"";
	private static final String TAG_STRING_END = "\">";

	private static final Pattern STRINGRESOURCEPATTERN = Pattern.compile(TAG_STRING_START + "([a-zA-Z_\\-]+)" + TAG_STRING_END);

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================
	
	public StringValuesChecker(final File projectResourceDir, final String[] pFolders) {
		super(projectResourceDir, pFolders);
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods from SuperClass/Interfaces
	// ===========================================================

	@Override
	protected String getBaseDirectory() {
		return BASEDIRECTORY;
	}	
	
	@Override
	protected FilenameFilter getFilenameFilter() {
		return new FilenameFilter(){
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".xml");
			}
		};
	}
	
	/**
	 * <b>NOTE</b> alternatively a XML-Parser could be used.
	 * @param pResourceFolder
	 * @param pFile
	 */
	@Override
	protected Map<String, List<String>> checkFile(final String pResourceFolder, final File pFile) {
		if(pFile == null)
			throw new IllegalArgumentException();

		final Map<String, List<String>> out = new HashMap<String, List<String>>();
		
		try {
			final Scanner s = new Scanner(pFile);

			while(s.hasNextLine()){
				final String line = s.nextLine();
				/* Check each line, if it is a String-Resource. */
				final Matcher m = STRINGRESOURCEPATTERN.matcher(line);
				if(m.find()){
					/* If something was found, extract the resourceName. */
					final String stringTag = m.group();
					final String name = stringTag.substring(TAG_STRING_START.length(), stringTag.length() - TAG_STRING_END.length());

					final List<String> existing = out.get(name);
					/* If no representations existed before, add a new List. */
					if(existing != null){
						existing.add(pResourceFolder);
					}else{
						final List<String> newList = new ArrayList<String>();
						newList.add(pResourceFolder);
						out.put(name, newList);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return out;
	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
